package com.jari.lg.service.analysis.event;


import com.jari.lg.domain.*;
import com.jari.lg.mapper.CrossBaseMapper;
import com.jari.lg.mapper.EventHisMapper;
import com.jari.lg.mapper.ParkingLotBaseMapper;
import com.jari.lg.mapper.SysDictDataMapper;
import com.jari.lg.utils.BaseData;
import com.jari.lg.utils.Utils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 版 权 : jariec.com
 * 包 名 : com.jari.lg.service.analysis.event
 * 描 述 : 生成事故事件数据
 * 创建 时 间: 2022/3/22 18:23
 * @author : lpf
 */
@Service
public class EventService {

    @Resource
    private EventHisMapper eventHisMapper;
    @Resource
    private SysDictDataMapper sysDictDataMapper;
    @Resource
    private CrossBaseMapper crossBaseMapper;
    @Resource
    private ParkingLotBaseMapper parkingLotBaseMapper;

    @Scheduled(cron = "30 * * * * ? ")
    public void createEventHisData() throws ParseException {
        List<EventHis> result = new ArrayList<>();
        List<SysDictData> sysDictDataList = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            EventHis eventHis = new EventHis();
            Date date = randomDate("2021-01-01","2021-12-31");
            SysDictDataExample example = new SysDictDataExample();
            SysDictDataExample.Criteria criteria = example.createCriteria();
            criteria.andDicttypeEqualTo("EVENT_TRAFFIC_TYPE");
            sysDictDataList = sysDictDataMapper.selectByExample(example);
            String eventType = sysDictDataList.get(new Random().nextInt(sysDictDataList.size())).getDictdata();
            String date1 = new SimpleDateFormat("yyyy.MM.dd").format(date);
            String datetime = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(date);
            Date datetime1 = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").parse(datetime);
            eventHis.setId(Utils.getUUID());
            String eventName = null;
            CrossBaseExample crossBaseExample = new CrossBaseExample();
            CrossBaseExample.Criteria criteria1 = null;
            List<CrossBase> crossBases = new ArrayList<>();
            ParkingLotBaseExample parkingLotBaseExample = new ParkingLotBaseExample();
            ParkingLotBaseExample.Criteria criteria2 = null;
            List<ParkingLotBase> parkingLotBases = new ArrayList<>();
            DevBase devBase = new DevBase();
            CrossBase crossBase = new CrossBase();
            int eventId = eventHisMapper.selectMaxEventId(null);
            eventHis.setEventid(eventId + 1);
            switch (eventType) {
                // 故障事件
                case "1":
                    devBase = BaseData.devBaseList.get(new Random().nextInt(BaseData.devBaseList.size()));
                    eventName = date1 + "_" + "故障事件";
                    eventHis.setDevicecode(devBase.getDevicecode());
                    eventHis.setDevicename(devBase.getDevicename());
                    eventHis.setEventname(eventName);
                    eventHis.setEventtype(eventType);
                    if (devBase.getDevicetype().equals("04") && devBase.getCrosscode() == null) {
                        criteria2 = parkingLotBaseExample.createCriteria();
                        criteria2.andParkingidEqualTo(devBase.getParkinglotcode().toString());
                        parkingLotBases = parkingLotBaseMapper.selectByExample(parkingLotBaseExample);
                        eventHis.setLat(new BigDecimal(parkingLotBases.get(0).getParkinglat()));
                        eventHis.setLon(new BigDecimal(parkingLotBases.get(0).getParkinglon()));
                        eventHis.setLocation(devBase.getDevicename());
                    } else {
                        if (devBase.getCrosscode() == null) {
                            continue;
                        }
                        criteria1 = crossBaseExample.createCriteria();
                        criteria1.andCrosscodeEqualTo(devBase.getCrosscode());
                        crossBases = crossBaseMapper.selectByExample(crossBaseExample);
                        eventHis.setLat(crossBases.get(0).getCrosslat());
                        eventHis.setLon(crossBases.get(0).getCrosslon());
                        eventHis.setLocation(crossBases.get(0).getCrossname());
                    }
                    break;
                // 报警事件
                case "2":
                    DevBase ePolice = BaseData.ePoliceList.get(new Random().nextInt(BaseData.ePoliceList.size()));
                    eventName = date1 + "_" + "报警事件";
                    criteria1 = crossBaseExample.createCriteria();
                    criteria1.andCrosscodeEqualTo(ePolice.getCrosscode());
                    crossBases = crossBaseMapper.selectByExample(crossBaseExample);
                    eventHis.setDevicecode(ePolice.getDevicecode());
                    eventHis.setDevicename(ePolice.getDevicename());
                    eventHis.setEventname(eventName);
                    eventHis.setEventtype(eventType);
                    eventHis.setLat(crossBases.get(0).getCrosslat());
                    eventHis.setLon(crossBases.get(0).getCrosslon());
                    eventHis.setLocation(crossBases.get(0).getCrossname());
                    break;
                // 数据异常事件
                case "3":
                    devBase = BaseData.devBaseList.get(new Random().nextInt(BaseData.devBaseList.size()));
                    eventName = date1 + "_" + "数据异常事件";
                    eventHis.setDevicecode(devBase.getDevicecode());
                    eventHis.setDevicename(devBase.getDevicename());
                    eventHis.setEventname(eventName);
                    eventHis.setEventtype(eventType);
                    if (devBase.getDevicetype().equals("04") && devBase.getCrosscode() == null) {
                        criteria2 = parkingLotBaseExample.createCriteria();
                        criteria2.andParkingidEqualTo(devBase.getParkinglotcode().toString());
                        parkingLotBases = parkingLotBaseMapper.selectByExample(parkingLotBaseExample);
                        eventHis.setLat(new BigDecimal(parkingLotBases.get(0).getParkinglat()));
                        eventHis.setLon(new BigDecimal(parkingLotBases.get(0).getParkinglon()));
                        eventHis.setLocation(devBase.getDevicename());
                    } else {
                        if (devBase.getCrosscode() == null) {
                            continue;
                        }
                        criteria1 = crossBaseExample.createCriteria();
                        criteria1.andCrosscodeEqualTo(devBase.getCrosscode());
                        crossBases = crossBaseMapper.selectByExample(crossBaseExample);
                        eventHis.setLat(crossBases.get(0).getCrosslat());
                        eventHis.setLon(crossBases.get(0).getCrosslon());
                        eventHis.setLocation(crossBases.get(0).getCrossname());
                    }
                    break;
                //  配置优化事件
                case "4":
                    crossBase = BaseData.crossBaseList.get(new Random().nextInt(BaseData.crossBaseList.size()));
                    eventName = date1 + "_" + "配置优化事件";
                    eventHis.setDevicecode(crossBase.getCrosscode());
                    eventHis.setDevicename(crossBase.getCrossname());
                    eventHis.setEventname(eventName);
                    eventHis.setEventtype(eventType);
                    eventHis.setLat(crossBase.getCrosslat());
                    eventHis.setLon(crossBase.getCrosslon());
                    eventHis.setLocation(crossBase.getCrossname());
                    break;
                // 拥堵事件
                case "5":
                    crossBase = BaseData.crossBaseList.get(new Random().nextInt(BaseData.crossBaseList.size()));
                    eventName = date1 + "_" + "拥堵事件";
                    eventHis.setDevicecode(crossBase.getCrosscode());
                    eventHis.setDevicename(crossBase.getCrossname());
                    eventHis.setEventname(eventName);
                    eventHis.setEventtype(eventType);
                    eventHis.setLat(crossBase.getCrosslat());
                    eventHis.setLon(crossBase.getCrosslon());
                    eventHis.setLocation(crossBase.getCrossname());
                default:
                    break;
            }
            int _eventId = eventId + i + 1;
            eventHis.setEventid(_eventId);
            eventHis.setDatetime(datetime1);
            eventHis.setPlatenumber(null);
            eventHis.setPlatetype(null);
            eventHis.setCarcolor(null);
            eventHis.setDescribes(null);
            eventHis.setCreator(null);
            eventHis.setGmtcreate(null);
            eventHis.setModifier(null);
            eventHis.setGmtmodified(null);
            eventHis.setDatastatus(null);
            eventHis.setVersion(null);
            eventHis.setRemark(null);
            result.add(eventHis);
        }
        eventHisMapper.insertBatch(result);
    }

    /**
     * @描述 生成指定时间内的随机时间
     * @时间 2022/3/22 18:20
     * @参数
     * @返回值
     * @Author lpf
     */
    private static Date randomDate(String beginDate,String endDate){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date start = format.parse(beginDate);
            Date end = format.parse(endDate);

            if(start.getTime() >= end.getTime()){
                return null;
            }
            long date = random(start.getTime(),end.getTime());
            return new Date(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static long random(long begin,long end){
        long rtn = begin + (long)(Math.random() * (end - begin));
        if(rtn == begin || rtn == end) {
            return random(begin,end);
        }
        return rtn;
    }
}
